package com.ict.txmsv2.controller.learnResource;

import com.ict.txmsv2.model.LearnResouce;
import com.ict.txmsv2.model.MethodDescrition;
import com.ict.txmsv2.model.RespBean;
import com.ict.txmsv2.model.RespPageBean;
import com.ict.txmsv2.services.ClicksService;
import com.ict.txmsv2.services.LearnResouceService;
import com.ict.txmsv2.utils.SdfBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author yoleen
 * @date 2020/12/16 15:51
 */

@RestController
@RequestMapping("/resource/study")
public class LearnResController {
    @Autowired
    LearnResouceService learnResouceService;
    @Autowired
    ClicksService clicksService;

    @GetMapping("/")
    @MethodDescrition("分页查询学习资源")
    public RespPageBean getLearnResouceList(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10") Integer size, String category,String name,String uploadUser){
        return learnResouceService.getLearnResouceList(page,size,new LearnResouce(category, name, uploadUser));
    }

    @PutMapping("/click")
    @MethodDescrition("")
    public void click(Integer learnId,Integer userId){
        if (clicksService.check(learnId,userId)) {
            clicksService.add(learnId, userId);
            learnResouceService.updateRecord(learnId);
        }
    }

    @PostMapping("/")
    @MethodDescrition("上传资料")
    public RespBean uploadRecourse(@RequestBody LearnResouce learnResouce){
        String date = SdfBuilder.getSimpleDateFormat().format(new Date());
        learnResouce.setUploaddate(date);
        if(learnResouceService.uploadRecourse(learnResouce)==1){
            return RespBean.ok("恭喜你的资料进入我们的资料库");
        }
        return RespBean.error("哦豁，上传失败");
    }

    @DeleteMapping("/{id}")
    @MethodDescrition("删除学习资料")
    public RespBean deleteRecourse(@PathVariable int id) {
        if (learnResouceService.deleteRecourse(id) == 1) {
            return RespBean.ok("移除咯");
        }
        return RespBean.error("这么好的资料再多留会呗");
    }
}
